//index.js
//获取应用实例
const app = getApp()

Page({
  data: {
    motto: '赏金达人',
    userInfo: null,
    hasUserInfo: false,
    hasUserRun: false,
    is_share:0,
    single:true,
    unauthorized:false,
    modalHidden: true,
    canIUse: wx.canIUse('button.open-type.getUserInfo')
  },
  onShareAppMessage: function (e) {
    let that = this;
    that.setData({
      is_share: 1
    })
    wx.setStorageSync('share', 1)
    return {
      title: '赏金达人',
      // path: '/pages/form/form_table/form_table',
      imageUrl: 'https://zs.cdbbox.com/uploads/images/20200323194452.jpg',
      success: function (res) {
        //console.log("转发成功")
      },
    }
  },
  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  goTable: function (e) {
    let share=wx.getStorageSync('share')
    wx.getSetting({
      success: result => {
        //console.log(result)
        if (!result.authSetting['scope.userInfo']) {
          wx.openSetting({
            success(res) {
              res.authSetting = {
                "scope.werun": true,
                "scope.userInfo": true,
                "scope.writePhotosAlbum": true,
              }

            }
          })
        }else if (!result.authSetting['scope.werun']) {
          wx.openSetting({
            success(res) {
              res.authSetting = {
                "scope.werun": true,
                "scope.userInfo": true,
                "scope.writePhotosAlbum": true,
              }

            }
          })

          
        } else {
          if (share) {
            wx.navigateTo({
              url: "/pages/form/form_table/form_table"
            })
          } else {
            this.setData({
              modalHidden: false,
            })
            
          }
        }
      }
    })
    
    
    
  },
  modalCandel: function () {
    // do something
    this.setData({
      modalHidden: true
    })
  },

  /**
   *  点击确认
   */
  modalConfirm: function () {
    // do something
    wx.downloadFile({
     // url: 'https://zs.cdbbox.com/index/api/get_appcode',
      url: 'https://zs.cdbbox.com/uploads/images/20200323194452.jpg',
      success(res) {
        wx.saveImageToPhotosAlbum({
          filePath: res.tempFilePath,
          success(res) {
            //console.log(res)
          }
        })
      }
    })
    this.setData({
      is_share: 1,
      modalHidden: true
    })
    wx.setStorageSync('share', 1)
  },
  onLoad: function () {
    wx.authorize({
        scope: 'scope.writePhotosAlbum',
        complete() {

        }
      })

    if (app.globalData.userInfo) {
      wx.getSetting({
        success: result => {

          if (!result.authSetting['scope.userInfo']){
            
            this.setData({
              hasUserInfo: false
            })
          }else{
            this.setData({
              hasUserInfo: true
            })
          }


          if (!result.authSetting['scope.werun']) {
            this.setData({
              hasUserRun: false
            })
          } else {
            this.setData({
              hasUserRun: true
            })
          }
        }
      })
      this.setData({
        userInfo: app.globalData.userInfo,
      })
    } else if (this.data.canIUse){
      // 由于 getUserInfo 是网络请求，可能会在 Page.onLoad 之后才返回
      // 所以此处加入 callback 以防止这种情况
      app.userInfoReadyCallback = res => {
        this.setData({
          userInfo: res.userInfo,
          hasUserInfo: true
        })
      }
    } else {
      // 在没有 open-type=getUserInfo 版本的兼容处理
      wx.getUserInfo({
        success: res => {
          app.globalData.userInfo = res.userInfo
          this.setData({
            userInfo: res.userInfo,
            hasUserInfo: true
          })
        },fail:res=>{
          //console.log(res,777)
        }
      })
    }

    //判断用户是否有授权
    


  },
  getUserInfo: function(e) {
    //console.log(e)
    if (e.detail.userInfo){
      this.setData({
        userInfo: e.detail.userInfo,
        hasUserInfo: true
      })

      wx.login({
        success: res => {
          if (res.code) {
                wx.getUserInfo({ //得到rawData, signatrue, encryptData
                  success: function (data) {
                    var rawData = data.rawData;
                    var signature = data.signature;
                    var encryptedData = data.encryptedData;
                    var iv = data.iv;

                    wx.request({
                      url: 'https://zs.cdbbox.com/index/api/login',
                      data: {
                        "code": res.code,
                        "rawData": rawData,
                        "signature": signature,
                        'iv': iv,
                        'encryptedData': encryptedData,
                      },
                      method: 'POST',
                      success: function (info) {
                        let userInfo = info.data
                        app.globalData.userInfo  = userInfo
                        //console.log(userInfo)
                      }
                    })
                  }
                })
          }
        }
      })

      
    }else{
      wx.showModal({
        title: '警告',
        content: '您拒绝授权，无法报名，请授权之后再报名',
        showCancel: false,
        confirmText: '确定',
        success: function (res) {
          // 用户没有授权成功，不需要改变 isHide 的值
          if (res.confirm) {
            //console.log('用户点击了“返回授权”');
          }
        }
      });
    }


  }
})
